<?php


namespace app\admin\controller;

use app\model\shop\ShopCategory as ShopCategoryModel;
use app\model\shop\ShopGroup as ShopGroupModel;
use app\model\shop\ShopApply as ShopApplyModel;
use app\model\web\WebSite;

/**
 * 商家申请控制器
 */
class Localshopapply extends BaseAdmin
{

    /**
     * 商家申请
     */
    public function apply()
    {
        if (request()->isAjax()) {
            $page = input('page', 1);
            $page_size = input('page_size', PAGE_LIST_ROWS);
            $search_text = input('search_text', '');
            $search_text_user = input('search_text_user', '');
            $category_id = input('category_id', 0);
            $group_id = input('group_id', 0);
            $apply_state = input('apply_state', 0);
            $start_time = input("start_time", '');
            $end_time = input("end_time", '');
            $apply_no = input('apply_no','');

            $condition = [
                ['shop_type','=',1]
            ];

            $site_id = input('site_id', '');
            if ($site_id) {
                $condition[] = [ 'site_id', '=', $site_id ];
            }
            if ($apply_no) {
                $condition[] = [ 'apply_no', 'like', '%' . $apply_no . '%' ];
            }
            if ($search_text) {
                $condition[] = [ 'shop_name', 'like', '%' . $search_text . '%' ];
            }

            //$condition[] = [ 'username', 'like', '%' . $search_text_user . '%' ];
            if ($category_id != 0) {
                $condition[] = [ 'category_id', '=', $category_id ];
            }
            if ($group_id != 0) {
                $condition[] = [ 'group_id', '=', $group_id ];
            }
            if ($apply_state != 0) {
                if ($apply_state == 4) {
                    $condition[] = ['apply_state', '=', '2'];
                    $condition[] = ['paying_money_certificate', '=', ''];
                } else {
                    $condition[] = [ 'apply_state', '=', $apply_state ];
                }
            }
            if (!empty($start_time) && empty($end_time)) {
                $condition[] = [ 'create_time', '>=', date_to_time($start_time) ];
            } elseif (empty($start_time) && !empty($end_time)) {
                $condition[] = [ "create_time", "<=", date_to_time($end_time) ];
            } elseif (!empty($start_time) && !empty($end_time)) {
                $condition[] = [ 'create_time', 'between', [ date_to_time($start_time), date_to_time($end_time) ] ];
            }

            $order = 'create_time desc';
            //申请会员 店铺名称 申请状态名称  拒绝理由（apply_message） 入驻时长（apply_year）  分类名称（category_name） 分组名称（group_name）create_time(申请时间)
            $field = 'apply_id,member_id,member_name,cert_id,shop_name,apply_state,apply_message,avatar,category_name,apply_no,group_name,audit_time,finish_time,create_time,username,mobile,address,full_address';

            $shop_apply_model = new ShopApplyModel();
            $res = $shop_apply_model->getApplyPageList($condition, $page, $page_size, $order, $field);

            //处理审核状态
            $apply_state_arr = $shop_apply_model->getApplyState();
            foreach ($res['data']['list'] as $key => $val) {
                if ($apply_state == 2) {
                    if (empty(trim($val['paying_money_certificate']))) {
                        $res['data']['count'] = $res['data']['count'] - 1;
                        unset($res['data']['list'][$key]);
                        continue;
                    }
                }
                $res['data']['list'][ $key ]['apply_state_name'] = $apply_state_arr[ $val['apply_state'] ];
            }
            if ($apply_state == 2) {
                if (empty($res['data']['count'])) {
                    $res['data']['page_count'] = 0;
                } else {
                    $res['data']['page_count'] = ceil($res['data']['page_count']/$page_size);
                }
            }

            return $res;

        } else {
            //商家主营行业
            $shop_category_model = new ShopCategoryModel();
            $shop_category_list = $shop_category_model->getCategoryList([], 'category_id, category_name', 'sort asc');
            $this->assign('shop_category_list', $shop_category_list['data']);

            //店铺等级
            $shop_group_model = new ShopGroupModel();
            $shop_group_list = $shop_group_model->getGroupList([['is_own','=',0]], 'group_id,is_own,group_name,fee,remark', 'is_own asc,fee asc');
            $this->assign('shop_group_list', $shop_group_list['data']);

            //申请状态
            $shop_apply_model = new ShopApplyModel();
            $apply_state_arr = $shop_apply_model->getApplyState();
            $this->assign('apply_state_arr', $apply_state_arr);

            $is_addon_city = addon_is_exit('city');
            $this->assign('is_addon_city',$is_addon_city);

            if($is_addon_city == 1){
                $website_model = new WebSite();
                $website_list = $website_model->getWebsiteList([],'site_id,site_area_name');
                $this->assign('website_list',$website_list['data']);
            }

            return $this->fetch('localshopapply/apply');
        }
    }

    /**
     * 申请详情
     */
    public function applyDetail()
    {
        $apply_id = input('apply_id', 0);

        $shop_apply_model = new ShopApplyModel();
        $apply_detail = $shop_apply_model->getApplyInfo([ [ 'apply_id', '=', $apply_id ] ]);

        $val = $shop_apply_model->transformSeckillTime($apply_detail['data']);
        $apply_detail['data'][ 'start_time_show' ] = "{$val['start_hour']}:{$val['start_minute']}";
        $apply_detail['data'][ 'end_time_show' ] = "{$val['end_hour']}:{$val['end_minute']}";
        $apply_detail['data'][ 'banner' ] = explode(',',$apply_detail['data'][ 'banner' ]);
        $this->assign('apply_detail', $apply_detail['data']);

        return $this->fetch('localshopapply/apply_detail');
    }
}